package practise.trackerpoc.server;

import static com.googlecode.objectify.ObjectifyService.ofy;

import java.util.ArrayList;
import java.util.logging.Logger;

import practise.trackerpoc.client.SearchService;
import practise.trackerpoc.shared.MemberProp;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.cmd.Query;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class SearchServiceImpl extends RemoteServiceServlet implements
		SearchService {
	
	private Logger logger;
	
	static {
		ObjectifyService.factory().register(Member.class);
		ObjectifyService.factory().register(Center.class);
	}
	
	public SearchServiceImpl() {
		logger = Logger.getLogger(SearchServiceImpl.class.getName());
	}
	
	@Override
	public ArrayList<MemberProp> getMatchingMembers(String searchString) {
		
		MemberProp memberProp = new MemberProp();
		memberProp.nameFirst = "Sathya";
		
		//this will be picked up from center
		Center center = new Center();
		center.id = 1L;
		//center.name = "Singapore";
						
		//search by first name or last name or phone or email
		searchString = searchString.toLowerCase();
		
		long startTime = System.currentTimeMillis();
		
		Query<Member> members = ofy().load().type(Member.class).filter("centers", center);
		
		logger.info("fetched " + members.count() + " members for center - " + center +
				" in " + (System.currentTimeMillis() - startTime) + " ms. " +
				". searching for " + searchString + "...");
		
		startTime = System.currentTimeMillis();
		
		ArrayList<MemberProp> memberProps = new ArrayList<MemberProp>();	
		
//		for (Member member : members) {
//			if (((null != member.nameFirst) && member.nameFirst.startsWith(searchString)) ||
//					((null != member.nameLast) && member.nameLast.startsWith(searchString)) ||
//					((null != member.email1) &&member.email1.startsWith(searchString)) ||
//					((null != member.email2) && member.email2.startsWith(searchString)) ||
//					((null != member.phoneHome) && member.phoneHome.phoneNumber.startsWith(searchString)) ||
//					((null != member.phoneOffice) && member.phoneOffice.phoneNumber.startsWith(searchString)) ||
//					((null != member.phoneMobile) && member.phoneMobile.phoneNumber.startsWith(searchString)) ||
//					((null != member.phoneAlternate) && member.phoneAlternate.phoneNumber.startsWith(searchString))) {
//				
//				memberProps.add(member.toMemberProp());
//			}
//		}		
//		
//		logger.info("found " + memberProps.size() + " match(es) in " + 
//				(System.currentTimeMillis() - startTime) + " ms for starting pattern match.");
		
		memberProps = new ArrayList<MemberProp>();
		startTime = System.currentTimeMillis();
		
		for (Member member : members) {
			if (((null != member.nameFirst) && member.nameFirst.contains(searchString)) ||
					((null != member.nameLast) && member.nameLast.contains(searchString)) ||
					((null != member.email1) &&member.email1.contains(searchString)) ||
					((null != member.email2) && member.email2.contains(searchString)) ||
					((null != member.phoneHome) && member.phoneHome.phoneNumber.contains(searchString)) ||
					((null != member.phoneOffice) && member.phoneOffice.phoneNumber.contains(searchString)) ||
					((null != member.phoneMobile) && member.phoneMobile.phoneNumber.contains(searchString)) ||
					((null != member.phoneAlternate) && member.phoneAlternate.phoneNumber.contains(searchString))) {
				
				memberProps.add(member.toMemberProp());
			}
		}		
		
		logger.info("found " + memberProps.size() + " match(es) in " + 
				(System.currentTimeMillis() - startTime) + " ms for full pattern match.");
		
		return memberProps;
	}
}
